package com.cwj.lab4.SORT;

import com.cwj.lab4.Tool;

import java.util.Arrays;

/**
 * 冒泡排序
 * 由大到小排序
 * */
public class BubbleSort<T extends Comparable<T>>implements Sorted<T> {
    public void Sort(T[]nums){
        boolean flag=false;
        for (int i = 0; i <nums.length-1 ; i++) {
            for (int j = 0; j < nums.length-i-1; j++) {
                if(nums[j+1].compareTo(nums[j])<0){
                    Tool.swap(nums,j,j+1);
                    flag=true;
                }
            }
            if(!flag){
                break;
            }else{
                flag=false;
            }
        }
    }

    public static void main(String[] args) {
        int size=30;
        Integer[]nums=new Integer[size];
        Integer[]testNum=new Integer[size];
        for (int i = 0; i <size ; i++) {
            testNum[i]=nums[i]=(int)(Math.random()*100);
        }
//        Integer[]nums={63, 39, 65, 6, 19};
        System.out.println(Arrays.toString(nums));
        HeapSort<Integer> mySort=new HeapSort<>();
        mySort.Sort(nums);
        Arrays.sort(testNum);
        System.out.println(Arrays.toString(nums));
        System.out.println(Arrays.toString(testNum));
    }

}
